package com.mysoft.gfyx.marketdevelop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetRecommendSearchTemplateDetailResponseDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetRecommendSearchTemplateResponseDto;
import com.mysoft.gfyx.marketdevelop.dao.RecommendSearchTemplateCategoryDao;
import com.mysoft.gfyx.marketdevelop.dao.RecommendSearchTemplateDao;
import com.mysoft.gfyx.marketdevelop.dao.entity.*;
import com.mysoft.gfyx.marketdevelop.service.RecommendSearchTemplateService;
import com.mysoft.gfyx.marketdevelop.service.dto.RecommendSearchTemplateCategoryDto;
import com.mysoft.gfyx.marketdevelop.service.dto.RecommendSearchTemplateDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
* 描述：RecommendSearchTemplate 业务代码
* @author JavaSdk generator
* @date 2022-07-22
*/
@Service
public class RecommendSearchTemplateServiceImp extends AppService implements RecommendSearchTemplateService {

    @Resource
    RecommendSearchTemplateDao recommendSearchTemplateDao;

    @Resource
    RecommendSearchTemplateCategoryDao recommendSearchTemplateCategoryDao;


    /**
     * 只给getRecommendSearchTemplateDetail用，会增加统计使用数
     * @param recommendSearchTemplateGUID
     * @return
     */
    @Override
    public GetRecommendSearchTemplateDetailResponseDto getRecommendSearchTemplateDetail(UUID recommendSearchTemplateGUID) {
        //获取推荐模板
        RecommendSearchTemplate recommendSearchTemplate = recommendSearchTemplateDao.selectById(recommendSearchTemplateGUID);
        if (recommendSearchTemplate == null) {
            return new GetRecommendSearchTemplateDetailResponseDto();
        }
        //使用次数+1
        this.addRecommendSearchTemplateServiceUseCount(recommendSearchTemplate);
        GetRecommendSearchTemplateDetailResponseDto getRecommendSearchTemplateDetailResponseDto = MappingUtil.getSimpleMapperFacade().map(recommendSearchTemplate, GetRecommendSearchTemplateDetailResponseDto.class);
        getRecommendSearchTemplateDetailResponseDto.setFilterJson(JSON.parseObject(getRecommendSearchTemplateDetailResponseDto.getFilter()));
        getRecommendSearchTemplateDetailResponseDto.setFilter(null);
        return getRecommendSearchTemplateDetailResponseDto;
    }

    @Override
    public boolean addRecommendSearchTemplateServiceUseCount(RecommendSearchTemplate recommendSearchTemplate) {
        recommendSearchTemplate.setUseCount(recommendSearchTemplate.getUseCount()+1);
        recommendSearchTemplateDao.updateById(recommendSearchTemplate);
        return true;
    }

    @Override
    public GetRecommendSearchTemplateResponseDto getRecommendSearchTemplate() {
        //获取所有分类
        GetRecommendSearchTemplateResponseDto getRecommendSearchTemplateResponseDto = new GetRecommendSearchTemplateResponseDto();
        QueryWrapper<RecommendSearchTemplateCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("Sort");
        queryWrapper.eq("IsEnable", 1);
        List<RecommendSearchTemplateCategory> recommendSearchTemplateCategoryList = recommendSearchTemplateCategoryDao.selectList(queryWrapper);
        List<RecommendSearchTemplateCategoryDto> recommendSearchTemplateCategoryDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(recommendSearchTemplateCategoryList, RecommendSearchTemplateCategoryDto.class);
        getRecommendSearchTemplateResponseDto.setRecommendSearchTemplateCategoryDtoList(recommendSearchTemplateCategoryDtoList);

        //获取所有推荐模板
        QueryWrapper<RecommendSearchTemplate> recommendSearchTemplateQueryWrapper = new QueryWrapper<>();
        recommendSearchTemplateQueryWrapper.orderByDesc("Sort");
        recommendSearchTemplateQueryWrapper.eq("IsEnable", 1);
        recommendSearchTemplateQueryWrapper.select("EntityType,Name,Sort,IsEnable,RecommendSearchTemplateGUID,recomSearchTemplateCategoryGUID,UseCount");
        List<RecommendSearchTemplate> recommendSearchTemplateList = recommendSearchTemplateDao.selectList(recommendSearchTemplateQueryWrapper);
        List<RecommendSearchTemplateDto> recommendSearchTemplateDtoList = MappingUtil.getSimpleMapperFacade().mapAsList(recommendSearchTemplateList, RecommendSearchTemplateDto.class);
        for (RecommendSearchTemplateDto recommendSearchTemplateDto : recommendSearchTemplateDtoList) {
            recommendSearchTemplateDto.setFilter(null);
        }
        getRecommendSearchTemplateResponseDto.setRecommendSearchTemplateDtoList(recommendSearchTemplateDtoList);
        return getRecommendSearchTemplateResponseDto;
    }
}
